package com.fire.control.system.module.device.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fire.control.system.dto.DeviceDTO;
import com.fire.control.system.dto.DeviceListDTO;
import com.fire.control.system.dto.GatewayListDTO;
import com.fire.control.system.dto.UserDTO;
import com.fire.control.system.entity.Area;
import com.fire.control.system.entity.Company;
import com.fire.control.system.entity.DeviceGroup;
import com.fire.control.system.entity.Gateway;
import com.fire.control.system.entity.User;
import com.fire.control.system.module.company.dao.CompanyMapper;
import com.fire.control.system.module.company.service.CompanyService;
import com.fire.control.system.module.device.dao.DeviceGroupMapper;
import com.fire.control.system.module.device.dao.DeviceMapper;
import com.fire.control.system.module.device.dao.GatewayMapper;
import com.fire.control.system.module.device.dao.GatewayRepository;
import com.fire.control.system.module.device.service.GatewayService;
import com.fire.control.system.module.user.dao.UserMapper;
import com.fire.control.system.statics.Message_Response;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.device.DeviceRequestProtocol;
import com.fire.control.system.support.util.ResultPageUtil;
import com.fire.control.system.support.util.XdaoDate;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 * 
 * @ClassName:  GatewayServiceImpl
 * @Description:  设备管理业务实现类
 * @author:  xiechengguang
 * @date:  2017年5月6日 上午9:39:40
 *
 */
@Service
public class GatewayServiceImpl implements GatewayService {
	@Autowired private CompanyMapper companyMapper;
	@Autowired private GatewayRepository gatewayRepository;
	@Autowired private DeviceGroupMapper deviceGroupMapper;
	@Autowired private GatewayMapper gatewayMapper;
	@Autowired private UserMapper userMapper;
	@Autowired private ProtocolSelector protocolSelector;
	@Autowired private CompanyService companyService;
	
	
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: getGatewayList</p> 
	 * <p>Description: 获取网关列表</p> 
	 * @param requestProtocol
	 * @return 
	 * @see com.fire.control.system.module.device.service.GatewayService#getGatewayList(com.fire.control.system.support.protocol.device.DeviceRequestProtocol)
	 */
	@Override
	public ResponseProtocol getGatewayList(DeviceRequestProtocol requestProtocol, UserDTO user) {
		ResponseProtocol respProtocol = protocolSelector.choose(requestProtocol);
		
		int page = requestProtocol.getCurrentPage();
		int size = requestProtocol.getPageSize();
		PageHelper.startPage(page, size);
		
		String companyId = user.getCompany().getId();
		List<GatewayListDTO> gatewayList = gatewayMapper.getGatewayList(companyId);
		
		respProtocol.setData(ResultPageUtil.resultPage(new PageInfo<GatewayListDTO>(gatewayList), gatewayList));
		return respProtocol;
		
	}
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: searchGateways</p> 
	 * <p>Description: 按条件搜索网关列表  </p> 
	 * @param requestProtocol
	 * @return 
	 * @see com.fire.control.system.module.device.service.GatewayService#searchGateways(com.fire.control.system.support.protocol.device.DeviceRequestProtocol)
	 */
	@Override
	public ResponseProtocol searchGateways(DeviceRequestProtocol requestProtocol, UserDTO user) {
		ResponseProtocol respProtocol = protocolSelector.choose(requestProtocol);
		
		int page = requestProtocol.getCurrentPage();
		int size = requestProtocol.getPageSize();
		PageHelper.startPage(page, size);
		
		String gatewayName = requestProtocol.getGatewayName();
		String companyId = user.getCompany().getId();
		
		List<GatewayListDTO> list = gatewayMapper.searchGateways(companyId, gatewayName);
		if(list != null && !list.isEmpty()) {
			respProtocol.setData(ResultPageUtil.resultPage(new PageInfo<GatewayListDTO>(list), list));
			return respProtocol;
		}
		ResponseMessage.CODE_204.messageToProtocol(respProtocol);
		respProtocol.setDate(new XdaoDate());
		return respProtocol;
	}
	
	
	/**
	 * 
	 * @Title: getGatewayInfo   
	 * @Description: 获取网关详细信息  
	 * @param:  requestProtocol      
	 * @return: ResponseProtocol      
	 * @throws
	 */
	@Override
	public ResponseProtocol getGatewayInfo(DeviceRequestProtocol requestProtocol) {
		ResponseProtocol respProtocol = protocolSelector.choose(requestProtocol);
		String id = requestProtocol.getId();
		if(null != id){
			GatewayListDTO gatewayDTO = gatewayMapper.getDetailById(id);
			if(null != gatewayDTO) {
				String region = gatewayDTO.getMergerName();
				gatewayDTO.setRegion(region.split(","));
				ResponseMessage.CODE_200.messageToProtocol(respProtocol);
				respProtocol.setData(gatewayDTO);
				return respProtocol;
			}
		}
		ResponseMessage.CODE_400.messageToProtocol(respProtocol);
		return respProtocol;
	}
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: updateGatewayInfo</p> 
	 * <p>Description: 修改网关信息 </p> 
	 * @param requestProtocol
	 * @return 
	 * @see com.fire.control.system.module.device.service.GatewayService#updateGatewayInfo(com.fire.control.system.support.protocol.device.DeviceRequestProtocol)
	 */
	@Transactional
	@Override
	public ResponseProtocol updateGatewayInfo(DeviceRequestProtocol requestProtocol) {
		ResponseProtocol respProtocol = protocolSelector.choose(requestProtocol);
		String id = requestProtocol.getId();
		if(id != null && id != "") {
			Gateway gateway = gatewayMapper.getById(id);
			if(null != gateway) {
				gateway = this.setGateway(gateway, requestProtocol);
				gatewayMapper.updateGateway(gateway);
				
				ResponseMessage.CODE_200.messageToProtocol(respProtocol);
				respProtocol.setDate(new XdaoDate());
				return respProtocol;
			}
		}
		ResponseMessage.CODE_400.messageToProtocol(respProtocol);
		respProtocol.setDate(new XdaoDate());
		return respProtocol;
	}
	
	/**
	 * @throws IOException 
	 * 
	 * @Title: setGateway   
	 * @Description: 新增或者修改网关公共方法 
	 * @param:        
	 * @return: Gateway      
	 * @throws
	 */
	private Gateway setGateway(Gateway gateway, DeviceRequestProtocol requestProtocol) {
		
		gateway.setGatewayName(requestProtocol.getGatewayName());
		gateway.setGatewayEui(requestProtocol.getGatewayEui());
		Area area = gatewayMapper.getAreaById(requestProtocol.getAreaId());
		gateway.setArea(area);
		gateway.setAddress(requestProtocol.getAddress()); //详细地址
		
		gateway.setCityId(requestProtocol.getCityId());
		gateway.setProvinceId(requestProtocol.getProvinceId());
		
		//通过公司id查找到公司
		Company company = companyMapper.getById(requestProtocol.getCompanyId());
		gateway.setCompany(company);
		
		//通过设备组id查找到设备组
		DeviceGroup dg = deviceGroupMapper.findById(requestProtocol.getGroupId());
		gateway.setDeviceGroup(dg);
		
		// 安全管理员、巡检员
		User securityManager = userMapper.getUserById(requestProtocol.getManagerId());
		User securityPatrol = userMapper.getUserById(requestProtocol.getPatrolId());
		gateway.setSecurityManager(securityManager);
		gateway.setSecurityPatrol(securityPatrol);
		
		return gateway;
	}
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: addGateway</p> 
	 * <p>Description: 新增网关</p> 
	 * @param requestProtocol
	 * @return 
	 * @see com.fire.control.system.module.device.service.GatewayService#addGateway(com.fire.control.system.support.protocol.device.DeviceRequestProtocol)
	 */
	@Override
	public ResponseProtocol addGateway(DeviceRequestProtocol requestProtocol) {
		ResponseProtocol respProtocol = protocolSelector.choose(requestProtocol);
		if(null != requestProtocol) {
			Gateway gateway = new Gateway();
			gateway = this.setGateway(gateway, requestProtocol);
			// 默认为正常
			Gateway newGateway = gatewayRepository.save(gateway);
			if(null != newGateway) {
				ResponseMessage.CODE_200.messageToProtocol(respProtocol);
				respProtocol.setDate(new XdaoDate());
				return respProtocol;     
			}
		}
		ResponseMessage.CODE_400.messageToProtocol(respProtocol);
		respProtocol.setDate(new XdaoDate());
		return respProtocol;
	}
	
	/**
	 * 
	 * @Title: deleteGateway   
	 * @Description: 删除一个或一组网关，删除，实际上是将status改为false  
	 * @param:  requestProtocol     
	 * @return: ResponseProtocol      
	 * @throws
	 */
	@Override
	public ResponseProtocol deleteGateway(DeviceRequestProtocol requestProtocol) {
		ResponseProtocol respProtocol = protocolSelector.choose(requestProtocol);
		String[] ids = requestProtocol.getIds();
		if(null != ids && ids.length > 0) {
			int n = 0;
			for(String id : ids) {
				int i = gatewayMapper.deleteById(id);
				n = n + i;
			}
			if(n == ids.length) {
				ResponseMessage.CODE_200.messageToProtocol(respProtocol);
				respProtocol.setDate(new XdaoDate());
				return respProtocol;
			}
		}
		ResponseMessage.CODE_400.messageToProtocol(respProtocol);
		respProtocol.setDate(new XdaoDate());
		return respProtocol;
	}
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: getGatewaysByTreeId</p> 
	 * <p>Description: 根据省、市、区、设备组id及其搜索条件获取其下的对应网关</p> 
	 * @param requestProtocol
	 * @param user
	 * @return 
	 * @see com.fire.control.system.module.device.service.GatewayService#getGatewaysByTreeId(com.fire.control.system.support.protocol.device.DeviceRequestProtocol, com.fire.control.system.dto.UserDTO)
	 */
	@Override
	public ResponseProtocol getGatewaysByTreeId(DeviceRequestProtocol requestProtocol, UserDTO user) {
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
		String id = requestProtocol.getId();  // 省、市、区、设备组的id
		String gatewayName = requestProtocol.getGatewayName(); // 根据网关名称模糊匹配
		
		int page = requestProtocol.getCurrentPage();
		int size = requestProtocol.getPageSize();
		
		List<String> companyIds = this.companyService.getChildCompanyIdList(user.getCompany().getId());
		
		PageHelper.startPage(page, size);
		List<GatewayListDTO> gatewayList = gatewayMapper.getGatewaysByTreeId(id, companyIds, gatewayName);
		if(gatewayList != null && !gatewayList.isEmpty()) {
			response.setData(ResultPageUtil.resultPage(new PageInfo<GatewayListDTO>(gatewayList), gatewayList));
			return response;
		}
		ResponseMessage.CODE_204.messageToProtocol(response);
		
		return response;
	}
	


}
